package com.monitor.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.monitor.entity.ErpMoney;
import com.monitor.service.ErpMoneyService;
import com.monitor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Transactional
@RequestMapping("/erpMoney")
@Api(description="金额有关的")
public class ErpMoneyController {

    @Resource
    private ErpMoneyService erpMoneyService;

    @RequestMapping("/inertErpMoney")
    @ApiOperation("添加或者修改金额有关的")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "i required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> inertOrUpdateMoney(ErpMoney erpMoney) {
        Result<Integer>  result = new Result<>();
        try {
            Integer insert = erpMoneyService.insertOrUpdate(erpMoney)?1:0;
            result.setData(insert);
            result.setMsg("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("失败");
        }
        return  result;
    }
 @RequestMapping("/removeErpMoney")
    @ApiOperation("删除金额有关的")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "i required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<Integer> removeErpMoney(Integer id) {
        Result<Integer>  result = new Result<>();
        try {
            Integer insert = erpMoneyService.deleteById(id)?1:0;
            result.setData(insert);
            result.setMsg("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("删除失败");
        }
        return  result;
    }
 @RequestMapping("/selectErpMoneyByPage")
    @ApiOperation("根据客户查询所有金额相关分页")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "i required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
 Result<Page<ErpMoney>> inertOrUpdateMoney(@RequestParam(value = "page", defaultValue = "1") int page,
                                           @RequestParam(value="limit",defaultValue="10") int limit,Integer custId) {
     Result<Page<ErpMoney>>   result = new Result<>();
        try {
            Page<ErpMoney> page1=new Page<ErpMoney>(page,limit);
            Page<ErpMoney> carPage=erpMoneyService.selectPage(page1,new EntityWrapper<ErpMoney>().eq("customer_id",custId));

            result.setData(carPage);
            result.setMsg("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("失败");
        }
        return  result;
    }

}
